package com.zhouyu.lifeCycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

import java.lang.reflect.Field;

/**
 * copyright (C), 2023,北京同创永益科技发展有限公司
 *
 * @author xiaodi
 * @version 1.0.0
 * <author>                     <time>                 <version>                   <description>
 * xiaodi               2023/12/3 15:38           1.0.0                      实例化前调用
 * @program spring
 * @description 实例化前调用
 * @create 2023/12/3 15:38
 */
@Component
public class TestLifeCycleInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor, EnvironmentAware, EmbeddedValueResolverAware, Ordered {
		public static final String testBeanName = "testLifeCycle";
	/*BeanPostProcessor*/
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if(testBeanName.equals(beanName)){
			System.out.println("4、初始化前");
		}
		return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
	}
	/*BeanPostProcessor*/
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if(testBeanName.equals(beanName)){
			System.out.println("5、初始化后");
		}
		return InstantiationAwareBeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
	}
	/*InstantiationAwareBeanPostProcessor*/
	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		if(testBeanName.equals(beanName)){
			System.out.println("1、实例化前");
//			try {
//				return beanClass.newInstance();
//			} catch (InstantiationException | IllegalAccessException e) {
//				throw new RuntimeException(e);
//			}
		}
		return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);
	}
	/*InstantiationAwareBeanPostProcessor*/
	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		if(testBeanName.equals(beanName)){
			System.out.println("2、实例化后");
		}
		return InstantiationAwareBeanPostProcessor.super.postProcessAfterInstantiation(bean, beanName);
	}
	/*InstantiationAwareBeanPostProcessor*/
	@Override
	public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
		if(testBeanName.equals(beanName)){
			System.out.println("3、实例化后的其他处理");
			for (Field field : bean.getClass().getFields()) {
				if (field.isAnnotationPresent(TestLifeCycleInject.class)) {
					field.setAccessible(true);
					try {
						field.set(bean, "123");
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
				}
			}

		}
		return InstantiationAwareBeanPostProcessor.super.postProcessProperties(pvs, bean, beanName);
	}

	/**
	 * 获取环境变量
	 * @param environment
	 */
	@Override
	public void setEnvironment(Environment environment) {
		String zhouyu = environment.getProperty("zhouyu");
		System.out.println(zhouyu);
	}

	/**
	 * 回传占位符信息
	 * @param resolver
	 */
	@Override
	public void setEmbeddedValueResolver(StringValueResolver resolver) {
		String zhouyu = resolver.resolveStringValue("zhouyu");
		System.out.println(zhouyu);

	}

	/**
	 * 进行排序
	 * @return
	 */
	@Override
	public int getOrder() {
		return 10;
	}
}
